package com.njtoyo.taxi.admin.service.local_service.commodity.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.library.common.OperationRecordHelper;
import com.njtoyo.taxi.admin.mapper.master.entity.local_service.LifeMerchant;
import com.njtoyo.taxi.admin.mapper.master.entity.local_service.LifeMerchantCommodity;
import com.njtoyo.taxi.admin.mapper.master.mapper.local_service.LifeMerchantCommodityMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.local_service.LifeMerchantMapper;
import com.njtoyo.taxi.admin.rest.presenter.local_service.commodity.CommodityExamineDetailsPresenter;
import com.njtoyo.taxi.admin.rest.presenter.local_service.commodity.CommodityExaminePagePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.commodity.CommodityExaminePageWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.commodity.CommodityExamineWrapper;
import com.njtoyo.taxi.admin.service.local_service.commodity.CommodityExamineService;
import com.taxi.entity.common.RestResult;
import com.taxi.entity.common.ResultEnum;
import com.njtoyo.taxi.entity.backend.AdminUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.hutool.core.util.ObjectUtil.isEmpty;
import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
import static java.util.stream.Collectors.toList;

@Slf4j
@Service
public class CommodityExamineServiceImpl implements CommodityExamineService {

    @Autowired
    private OperationRecordHelper operationRecordHelper;

    @Autowired
    private LifeMerchantCommodityMapper lifeMerchantCommodityMapper;

    @Autowired
    private LifeMerchantMapper lifeMerchantMapper;

    /**
     * 商品审核列表
     *
     * @param adminUser
     * @param wrapper
     * @return
     */
    @Override
    public RestResult<Page<CommodityExaminePagePresenter>> getPage(AdminUser adminUser, CommodityExaminePageWrapper wrapper) {
        switch (adminUser.getAccountType()) {
            case platform:
                break;
            default:
                return RestResult.failed("账号权限不够");
        }
        Page<CommodityExaminePagePresenter> page = lifeMerchantCommodityMapper.getExaminePage(wrapper);
        List<CommodityExaminePagePresenter> list = page.getRecords();
        if (isNotEmpty(list)) {
            List<Long> merchantIds = list.stream().map(CommodityExaminePagePresenter::getMerchantId).collect(toList());
            List<Long> commodityCategoryIds = list.stream().map(CommodityExaminePagePresenter::getCommodityCategoryId).collect(toList());

            //商品类型id
            QueryWrapper<LifeMerchantCommodity> commodityWrapper = new QueryWrapper<>();
            commodityWrapper.select("id", "name");
            commodityWrapper.in("id", commodityCategoryIds);
            List<Map<String, Object>> maps = lifeMerchantCommodityMapper.selectMaps(commodityWrapper);
            Map<Long, String> map = maps.stream().collect(Collectors.toMap(i -> Long.valueOf(i.get("id").toString()), i -> i.get("name").toString()));


            //商户名称,自动审核id
            QueryWrapper<LifeMerchant> merchantQueryWrapper = new QueryWrapper<>();
            merchantQueryWrapper.select("id", "name", "commodity_auto_review");
            merchantQueryWrapper.in("id", merchantIds);
            List<LifeMerchant> merchantList = lifeMerchantMapper.selectList(merchantQueryWrapper);

            list.forEach(result -> {
                Long merchantId = result.getMerchantId();

                //商品类型id
                result.setCommodityCategoryName(map.getOrDefault(result.getCommodityCategoryId(), null));
                //商户
                merchantList.stream().filter(lifeMerchant -> lifeMerchant.getId().equals(merchantId)).forEach(lifeMerchant -> {
                    result.setMerchantName(lifeMerchant.getName());
                    result.setCommodityAutoReview(lifeMerchant.getCommodityAutoReview());
                });
            });
        }
        return RestResult.success(page);
    }

    /**
     * 商品审核详情
     *
     * @param id
     * @return
     */
    @Override
    public RestResult<CommodityExamineDetailsPresenter> details(Long id) {
        LifeMerchantCommodity lifeMerchantCommodity = lifeMerchantCommodityMapper.selectById(id);
        if (isEmpty(lifeMerchantCommodity)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }

        //商户信息
        Long merchantId = lifeMerchantCommodity.getMerchantId();
        LifeMerchant lifeMerchant = lifeMerchantMapper.selectById(merchantId);

        CommodityExamineDetailsPresenter result = new CommodityExamineDetailsPresenter();
        result.setLifeMerchant(lifeMerchant);
        result.setLifeMerchantCommodity(lifeMerchantCommodity);
        return RestResult.success(result);
    }

    /**
     * 商品审核状态
     *
     * @param id
     * @param wrapper
     * @return
     */
    @Override
    public RestResult examine(Long id, CommodityExamineWrapper wrapper) {
        LifeMerchantCommodity lifeMerchantCommodity = lifeMerchantCommodityMapper.selectById(id);
        if (isEmpty(lifeMerchantCommodity)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        new LambdaUpdateChainWrapper<>(lifeMerchantCommodityMapper)
                .eq(LifeMerchantCommodity::getId, id)
                .set(LifeMerchantCommodity::getState, wrapper.getState())
                .set(LifeMerchantCommodity::getUpdatedAt, new Date())
                .update();
        operationRecordHelper.addBusinessLog(String.format("审核了商品 [%d] 的信息", id));
        return RestResult.success();
    }
}
